package com.leexuan.ordersystem.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.leexuan.ordersystem.beans.Sort;
import com.leexuan.ordersystem.db.SortDB;
import com.leexuan.ordersystem.forms.SortForm;

public class AddSortAction extends Action {

	public AddSortAction() {
		super();
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {

		SortForm fm = (SortForm) form;

		Sort st = new Sort();
		st.setSid(fm.getSid());
		st.setName(fm.getName());

		SortDB db = new SortDB();

		try {
			db.getConnection();

			db.addSort(st);

			return mapping.findForward("success");

		} catch (Exception e) {

			return (new ActionForward(mapping.getInput()));
		} finally {
			try {
				db.closeConnection();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

}
